package com.zq.app.repository;


import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;

import com.zq.app.domain.Employee;
/**
 * Repository为spring data核心接口，不提供任何方法
 * 泛型第一个参数是要操作的实体对象，第二个参数是主键的类型
 * 1）标记接口：没有包含方法声明的接口 
 * 默认实现类：org.springframework.data.jpa.repository.support.SimpleJpaRepository
 * 2）集成了该接口的接口受spring管理
 * @author Lion
 *查询方法规则
 *findBy字段'运算符'
 *例如：
 *and findByLastNameAndFirstName  where x.lastName=?1 and x.firstName=?2
 *or  findByLastNameOrFirstName	  where x.lastName=?1 or x.firstName=?2
 *between findByStartDateBetween  where x.startDate between ?1 and ?2
 *lessthan findByAgeLessThan      where x.age < ?1
 *greaterthan  findByAgeGreaterThan where x.age >?1
 *after								where x.startDate >?1
 *before                            where x.startDate <?1
 *isnull  findByAgeIsNull           where x.age is null
 *isnotnull/notnull findByAgeNotNull where x.age is not null
 *
 *
 */
//@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)
public interface EmployeeRepository
	/*extends Repository<Employee,Integer>*/
	extends CrudRepository<Employee, Integer>
{
	public Employee findByName(String name);
	
//	where name like ?% and age <?
	public List<Employee> findByNameStartingWithAndAgeLessThan(String name,Integer age);
	
	//采用注解，可以不用遵循命名规则 表名等关键字为类名
	@Query("select e from Employee e where id=?1")
	public Employee getEmployeeById(Integer id);
	
	@Query("select e from Employee e where name=:name and id=:id")
	public Employee getEmployeeByParams1(@Param("name") String name,@Param("id") Integer id);
	
	@Query("select o from Employee o where o.name like %?1%")
	public List<Employee> queryLike1(String name);
	
	/**
	 * 原生sql
	 * @return
	 */
	@Query(nativeQuery=true,value="select count(1) from employee")
	public long getCount();
	
	@Modifying//允许修改
	@Query("update Employee o set o.age=:age where o.id=:id")
	public void update(@Param("id")Integer id,@Param("age") Integer age);
	
	
	
}
